import { createRouter, createWebHistory } from 'vue-router'
import { useStore } from '@/store'

const routes = [
    {
        path: '/',
        name: 'home',
        meta: {
            title: '东华塔陵园',
        },
        component: () => import('@/views/home/Home.vue')
    },
    {
        path: '/sweepGraveAppointment',
        name: 'sweepGraveAppointment',
        meta: {
            title: '预约扫墓',
            // requireAuth: true,
        },
        component: () => import('@/views/sweepGraveAppointment/SweepGraveAppointment.vue')
    },
    {
        path: '/buryAppointment',
        name: 'buryAppointment',
        meta: {
            title: '预约安葬',
            // requireAuth: true,
        },
        component: () => import('@/views/buryAppointment/BuryAppointment.vue')
    },
    {
        path: '/lookTombAppointment',
        name: 'lookTombAppointment',
        meta: {
            title: '预约看墓',
            // requireAuth: true,
        },
        component: () => import('@/views/lookTombAppointment/LookTombAppointment.vue')
    },
    // {
    //     path: '/informationList',
    //     name: 'informationList',
    //     meta: {
    //         title: '信息列表',
    //         requireAuth: true,
    //     },
    //     component: () => import('@/views/informationList/InformationList.vue')
    // },
    {
        path: '/navigate',
        name: 'navigate',
        meta: {
            title: '导航'
        },
        component: () => import('@/views/navigate/Navigate.vue')
    },
    {
        path: '/businessInquiry',
        name: 'businessInquiry',
        meta: {
            title: '墓位查询'
        },
        component: () => import('@/views/businessInquiry/BusinessInquiry.vue')
    },
    {
        path: '/renew',
        name: 'renew',
        meta: {
            title: '墓位续费'
        },
        component: () => import('@/views/businessInquiry/BusinessInquiry.vue')
    },
    {
        path: '/cloudCommemorate',
        name: 'cloudCommemorate',
        meta: {
            title: '实景云纪念'
        },
        component: () => import('@/views/cloudCommemorate/CloudCommemorate.vue')
    },
    {
        path: '/appointmentRecord',
        name: 'appointmentRecord',
        meta: {
            title: '预约记录'
        },
        component: () => import('@/views/appointmentRecord/AppointmentRecord.vue')
    },
    {
        path: '/renewalRecord',
        name: 'renewalRecord',
        meta: {
            title: '续费记录'
        },
        component: () => import('@/views/renewalRecord/RenewalRecord.vue')
    },
    
    // {
    //     path: '/cloudSacrifice',
    //     name: 'cloudSacrifice',
    //     meta: {
    //         title: '云祭祀',
    //         // requireAuth: true,
    //     },
    //     component: () => import('@/views/cloudSacrifice/CloudSacrifice.vue')
    // },
    // {
    //     path: '/cloudSacrificeBuildLibrary',
    //     name: 'cloudSacrificeBuildLibrary',
    //     meta: {
    //         title: '建馆',
    //         // requireAuth: true,
    //     },
    //     component: () => import('@/views/cloudSacrifice/cloudSacrificeBuildLibrary/CloudSacrificeBuildLibrary.vue')
    // },
    {
        path: '/cloudSacrificePersonalHall',
        name: 'cloudSacrificePersonalHall',
        meta: {
            title: '云祭祀',
            // requireAuth: true,
        },
        component: () => import('@/views/cloudSacrifice/cloudSacrificePersonalHall/CloudSacrificePersonalHall.vue')
    },
    
]
console.log(import.meta.env.BASE_URL)
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    // history: createWebHistory('/'),
    // 路由跳转之后滚动条滚到顶部
    scrollBehavior (to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition
        } else {
            // return { x: 0, y: 0 } vue2这种，vue3改成top
            return { top: 0 }
        }
    },
    routes
})

//路由判断登录 根据路由配置文件的参数
router.beforeEach((to, from, next) => {
    //改title
    if (to.meta.title) {
        document.title = to.meta.title as string
    }
    // next();
    console.log(to.matched)
    if (to.matched.some(record => record.meta.requireAuth)) { //判断该路由是否需要登录权限
        console.log('需要登录');
        let store = useStore()
        console.log(store.openid)
        if (store.openid) { //判断当前的token是否存在 ； 登录存入的token
            next();
        } else {
            next({
                path: '/',
                query: {
                    redirect: to.fullPath
                } //将跳转的路由path作为参数，登录成功后跳转到该路由
            })
        }
    } else {
        next();
    }
    // if (to.matched.some(record => record.meta.requireAuth)) { //判断该路由是否需要登录权限
    //     console.log('需要登录');
    //     if (sessionStorage.token) { //判断当前的token是否存在 ； 登录存入的token
    //         next();
    //     } else {
    //         next({
    //             path: '/login',
    //             query: {
    //                 redirect: to.fullPath
    //             } //将跳转的路由path作为参数，登录成功后跳转到该路由
    //         })
    //     }
    // } else {
    //     next();
    // }
});

export default router